/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.cancel;

import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bff.api.manager.assembler.IAssemblerManager;
import com.inspur.edp.bff.api.manager.assembler.IExtendCancelAssember;
import com.inspur.edp.bff.api.manager.context.CancelContext;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.bff.spi.assembler.AbstractAssemblerManager;

public class CancelAction extends AbstractFSAction<VoidActionResult> {

    public CancelAction() {
    }

    @Override
    protected void execute() {
        IExtendCancelAssember assember = getAssember();
        if (assember == null) {
            doDefaultCancel();
            return;
        }
        CancelContext cancelContext = new CancelContext();
        assember.doCancel(getContext(), cancelContext);
    }

    private void doDefaultCancel() {
        for (IStandardLcp lcp : getContext().getLcps().values()) {
            lcp.cancel();
        }
    }

    private IExtendCancelAssember getAssember() {
        IAssemblerManager assemblerManager = getContext().getAssemblerManager();
        if (assemblerManager == null || !(assemblerManager instanceof AbstractAssemblerManager))
            return null;
        return ((AbstractAssemblerManager) assemblerManager).getCancelAssember();
    }

}
